package com.AdvancedDemos.集合.单列集合;

import java.util.*;

public class Learn工具类Collections {
    public static void main(String[] args) {
        /*
         * Collections并不是集合,而是一个用来操作集合的工具类,
         *
         * Collections提供的常用静态方法:
         * public static <T> boolean addAll(Collection<? super T> c,T... elements) --- 给集合批量添加元素
         * public static void shuffle(List<?> list) --- 打乱List集合中的元素顺序,Set系列本身就是无序的无序打乱
         * public static <T> void sort(List<T> list) --- 对List集合中的元素进行升序排序
         * public static <T> void  sort(List<T>list,Comparator<? super T> c) --- 对List集合中元素,按照比较器对象指定的规则进行排序
         */

        List<String> names = new ArrayList<>();
        Collections.addAll(names, "张三", "王五", "李四", "张麻子");
        System.out.println(names);

        System.out.println("------------------");
        ArrayList<Integer> integers = new ArrayList<>();
        Integer[] temp = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        Collections.addAll(integers, temp);//第二个参数是可变参数,可以接受同种类型多个数据,也可以是数组,但是注意类型必须是引用数据类型
        System.out.println(integers);


        HashSet<String> set = new HashSet<>();
        System.out.println("------------------");
//        Collections.addAll(set,"1","1","1","1","2","2");
        String[] strs = {"1", "1", "1", "1", "2", "2"};
        Collections.addAll(set, strs);
        System.out.println(set);
        //注意对于Set而言,用addAll函数批量添加元素时,元素会受容器影响而自动去重,排序
        //当集合容器是HashSet时 --- 如果是自定义类泛型要重写hashCode()和equals()才能去重否则报错
        //当集合容器是TreeSet时 --- 如果是自定义类型想要实现按照属性值排序必须①自然排序:自定义类实现Comparable接口重写compareTo方法自定义排序规则
        // 或者② 比较器排序:new集合容器时第二个实参用匿名内部类实现Comparator<String>接口自定义排序规则

        ///////////////////////////////////////////////////////////////

        System.out.println("------------------");
        Collections.shuffle(integers);//打乱List集合中的元素顺序,Set系列本身就是无序的无序打乱
        System.out.println(integers);
        Collections.shuffle(names);
        System.out.println(names);
        System.out.println("------------------");

        ///////////////////////////////////////////////////////////////

        Collections.sort(integers);//Integer类型直接排序,无序使用比较器
        System.out.println(integers);
        System.out.println("------------------");


        List<Student1> students = new ArrayList<>();//自定义类需要继承Comparable接口或者使用Comparator匿名内部类制定比较规则
        students.add(new Student1("蜘蛛精", 23, 169.7));
        students.add(new Student1("紫霞", 22, 169.8));
        students.add(new Student1("紫霞", 22, 169.8));
        students.add(new Student1("至尊宝", 26, 165.5));
        Collections.sort(students);
        System.out.println(students);//[Student{name='紫霞', age=22, height=169.8}, Student{name='紫霞', age=22, height=169.8}, Student{name='蜘蛛精', age=23, height=169.7}, Student{name='至尊宝', age=26, height=165.5}] 按照年龄升序排序,List允许重复

        System.out.println("------------------");
        Collections.sort(students, /*new Comparator<Student1>() {
            @Override
            public int compare(Student1 o1, Student1 o2) {
                return Double.compare(o1.getHeight(), o2.getHeight());
            }
        }*/
                //简化为Lambda表达式
                (o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())
        );
        System.out.println(students);//[Student{name='至尊宝', age=26, height=165.5}, Student{name='蜘蛛精', age=23, height=169.7}, Student{name='紫霞', age=22, height=169.8}, Student{name='紫霞', age=22, height=169.8}] 按照身高升序排序

        ///////////////////////////////////////////////////////////////
        System.out.println("------------------");
        System.out.println(integers);
        Collections.reverse(integers);//颠倒List中的元素顺序
        System.out.println(integers);

    }
}

class Student1 implements Comparable<Student1> {

    private String name;
    private int age;
    private double height;

    public Student1() {
    }

    public Student1(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    @Override
    public int compareTo(Student1 o) {
        //this与o进行比较,默认升序
        //如果 返回正整数 认为this > o
        //如果 返回负整数 认为this < o
        //如果 返回整数0 认为this == o
        return this.age - o.age;//升序
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return age == student1.age && Double.compare(student1.height, height) == 0 && Objects.equals(name, student1.name);
    }//只要两个对象内容一样,就返回true

    @Override
    public int hashCode() {
        return Objects.hash(name, age, height);
    }//只要两个对象内容一样,返回的哈希值就是一样的,因为是根据姓名,年龄,身高来计算哈希值的

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", age=" + age + ", height=" + height + '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}
